<?php

/*
 * Action 5$
 *
 * +1 Action
 * Name a card. Reveal cards from the top of your deck until you reveal a Victory card that is not the named card. Discard the other cards. Trash the Victory card and gain a Victory card costing up to 3 Coin more than it.
 */

class Rebuild extends Card implements iAction, iKingdom, iDarkAges {

    public function __construct() {
        parent::__construct("Rebuild", 5);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "+1 Action",
            "Name a card. Reveal cards from the top of your deck until you reveal a Victory card that is not the named card. Discard the other cards. Trash the Victory card and gain a Victory card costing up to 3 Coin more than it.");
    }

    public function play(Game $game, $isRealCard) {
        $game->addActions(1);
        ScreenDisplay::addScreenMessage($game, true, "get +1 action", "gets +1 action");

        $board_cards = $game->getAllBoardCards();
        ksort($board_cards);
        
        $output = MySocket::GetUserReply($game, Input_ChooseOne, $board_cards, 1, 1, null, "Name a card");
        $named_card = n2c($output[0]);
        ScreenDisplay::addScreenMessage($game, false, ", name " . nicePrint($named_card) , ", names ".ScreenDisplay::$LMSG);
        $named_card_key = $named_card->getKey();
        //only looking on the top card
        $card = $game->peekCardFromDrawDeck();
        $new_card = null;
        $discardCards = array();
        while ($card != null) {
            if ($card instanceof iVictory && $card->getKey() != $named_card_key) {
                $game->drawCards(1);
                $game->moveCardFromPileToPile($card, PlayHand, Trash);
                $price = $card->getPrice($game);
                $max_price = $price->add(3);
                $optional_cards = $game->getBoardVictoryCardsByPrice(new Price(0), $max_price);
                if (sizeof($optional_cards) > 0) {
                    $output = MySocket::GetUserReply($game, Input_ChooseOne, $optional_cards, 1, 1, null, "Choose a card to gain instead of ".$card->getValue());

                    $new_card_name = $output[0];
                    $new_card = $game->moveCardFromPileToPile($new_card_name, BoardCards, DiscardPile);
                }
                break;
            } else {
                $card = $game->removeCardFromDrawDeck();
                $discardCards[] = $card;
            }
            $card = $game->peekCardFromDrawDeck();
        }
        $game->discardTempDiscardCards($discardCards);
        ScreenDisplay::addScreenMessage($game, false, ", reveal " . nicePrint($discardCards) . " , trash " . nicePrint($card) . " and gain " . nicePrint($new_card), " reveals " . nicePrint($discardCards) . " , trashes " . nicePrint($card) . " and gains " . nicePrint($new_card));
        $game->cardActionFinished();
        return true;
    }

}

?>